package com.caiyouxi.goods.service;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.goods.mapper.GoodsVersionMapper;
import com.caiyouxi.goods.model.GoodsVersion;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/11/9.
 */
@Service
public class GoodsVersionsService extends BaseService{


    private Logger logger = Logger.getLogger(GoodsVersionsService.class);

    @Autowired
    private GoodsVersionMapper goodsVersionMapper;



    /**
     * 查询商品历史版本
     */
    public ResData get_goods_versions_info(GoodsVersion goodsVersion){
        String redis_key = "GOODS_VERSIONS_"+goodsVersion.getGoods_id()+goodsVersion.getVersion();
        logger.info("search redis...");
        GoodsVersion newGoodsVersion = (GoodsVersion) redisService.get(redis_key);
        if (null == newGoodsVersion){
            logger.info("search db...");
            newGoodsVersion  =goodsVersionMapper.get_goods_version(goodsVersion);
            redisService.set(redis_key,newGoodsVersion);
        }
        return  success(newGoodsVersion);
    }

    /**
     * 查询商品历史版本列表
     */
    public ResData get_goods_versions_list(GoodsVersion goodsVersion){
        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null != reids_page_size){
            goodsVersion.setPage_size(reids_page_size);
        }

        List<GoodsVersion> goodsDiscounts = goodsVersionMapper.get_goods_versions_list(goodsVersion);
        int total = goodsVersionMapper.get_goods_versions_count(goodsVersion);
        QueryParams pageList = new QueryParams();
        pageList.setTotal(total);
        pageList.setPage(goodsVersion.getPage());
        pageList.setPage_size(goodsVersion.getPage_size());
        pageList.setList(goodsDiscounts);
        return success(pageList);
    }

    /**
     * 新增商品历史版本
     */
    @Transactional
    public ResData add_goods_versions(GoodsVersion goodsVersion){
        int result =goodsVersionMapper.add_goods_version(goodsVersion);
        if(result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSVERSION_ADD_ERROR.getCode(),ErrorCode.SYS_GOOdSVERSION_ADD_ERROR.getMsg(),null);
    }

    /**
     * 修改商品历史版本
     */
    @Transactional
    public ResData update_goods_versions(GoodsVersion goodsVersion){
        int result = goodsVersionMapper.update_goods_version(goodsVersion);
        if(result == 1){
            String redis_key = "GOODS_VERSIONS_"+goodsVersion.getGoods_id()+goodsVersion.getVersion();
            redisService.set(redis_key,goodsVersion);  //更新缓存内容
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSVERSION_UPDATE_ERROR.getCode(),ErrorCode.SYS_GOOdSVERSION_UPDATE_ERROR.getMsg(),null);
    }

    /**
     * 删除商品历史版本
     */
    @Transactional
    public ResData delete_goods_versions(GoodsVersion goodsVersion){
        int result =goodsVersionMapper.delete_goods_version(goodsVersion);
        if(result == 1){
            String redis_key = "GOODS_VERSIONS_"+goodsVersion.getGoods_id()+goodsVersion.getVersion();
            redisService.remove(redis_key);  //删除缓存
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSVERSION_DELTET_ERROR.getCode(),ErrorCode.SYS_GOOdSVERSION_DELTET_ERROR.getMsg(),null);
    }





}
